<?php
/**
 * Created by PhpStorm.
 * User: wb-liuyuguang
 * Date: 14-5-19
 * Time: 下午1:29
 */

session_start();
header("content-type:text/html; charset=utf-8");
$id = $_POST['id'];
$row = $_SESSION['conf'][$id];
$website = $row[3];
$usr = $row[4];
$pwd = $row[5];
$txt = $row[6];
$lines = explode(";", $txt);

require('website.php');
$host = new GitWebsite();

$done = "成功啦";
if( $host->login($website, $usr, $pwd) !== false ){
    foreach($lines as $line){
        $line = trim($line);
        if( strlen($line) > 0 ){
            $rule = explode(",", $line);
            $src = trim($rule[0]);
            $output = trim($rule[1]);
			$zip = false;
			if( strcasecmp(trim($rule[2]), 'true') == 0){
				$zip = true;
			}
			if( $zip === false ){
			    switch($output){
			    case '':
			    case '.':
			    case '/':
			    case "\\":
			        $parts = explode("/", $src);
					$output = "./".$parts[count($parts) -1];
			    	break;
			    default:
			    	$end = substr( $output, strlen($output)-1 );
					if( $end === '/' || $end === "\\" ){
						$parts = explode("/", $src);
						$output = $output.$parts[count($parts) -1];
					}
			    	break;
			    }

			}
            if( $host->deploy($src, $output, $zip) === false ){
                $done = "失败了! 在部署<i>".$src."</i>到<i>".$output."</i>时发生错误。";
                break;
            }
            else{
                $done = $done."<br>成功部署<i>".$src."</i>到<i>".$output;
            }
        }
    }
}

echo $done;

?>